L = 8 
K = 5
day = 24*60
lastMin = day - 1
minutes = 0:lastMin

lambda = function(t) { 100 + 50/exp((t - 420)^2/(3600*L)) + 100/exp((L*(t - 480 - 30*L)^2)/360000) }
lambdaMax = as.numeric(optimize(f=lambda, interval=minutes, maximum=TRUE)[2])
probabilityFunction = function (t) { lambda(t) / lambdaMax }

arrivalTime = 0
arrivalTimes = numeric()
eventsInMinute = numeric(day)

while (arrivalTime < day) {
	interarrivalTime = rexp(1, rate=lambdaMax)
	arrivalTime = arrivalTime + interarrivalTime
	if (runif(1) <= probabilityFunction(arrivalTime)) {
	  arrivalTimes = c(arrivalTimes, arrivalTime)
	  indx = arrivalTime %/% 1 + 1
	  eventsInMinute[indx] = eventsInMinute[indx] + 1
	}
}

plot(x=minutes,y=lambda(minutes),lwd=4, type='l',xlab="Čas v minutách",ylab="Příchody za minutu")
# prvnich 50 prichodu na server
plot(x=arrivalTimes[1:(K*10)],y=numeric(K*10),xlab="Čas v minutách",ylab="")
# prichody na server behem celeho dne
plot(x=minutes,y=eventsInMinute,lwd=1, type='l',xlab="Čas v minutách",ylab="Příchody za minutu")
lines(x=minutes,lambda(minutes), lwd=4, col='red')
